package com.ruoyi.ai.controller;

import com.alibaba.dashscope.utils.JsonUtils;
import com.ruoyi.ai.domain.VolunteerForm;
import com.ruoyi.ai.domain.VolunteerInfo;
import com.ruoyi.ai.domain.VolunteerLog;
import com.ruoyi.ai.service.impl.VolunteerLogServiceImpl;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 志愿填报模块
 *
 * @author zyf
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/volunteer")
public class VolunteerController extends BaseController {
    @Autowired
    private VolunteerLogServiceImpl volunteerLogService;
    /**
     * 志愿填报
     */
    @PostMapping("/addVolunteer")
    public AjaxResult addVolunteer(@RequestBody VolunteerForm volunteerForm){
        List<VolunteerInfo> volunteerInfo = volunteerForm.getVolunteerInfo();
        AtomicInteger i= new AtomicInteger();
//        给志愿排序
        List<VolunteerInfo> collect = volunteerInfo.stream().map(e -> {
            e.setMajorNo(i.incrementAndGet());
            return e;
        }).collect(Collectors.toList());
        VolunteerLog volunteerLog = new VolunteerLog();
        volunteerLog.setName(volunteerForm.getStudentInfo().getName());
        volunteerLog.setNo(Long.valueOf(volunteerForm.getStudentInfo().getNo()));
        volunteerLog.setCreateUserId(SecurityUtils.getUserId());
        volunteerLog.setVolunteerInfo(JsonUtils.toJson(collect));
        volunteerLogService.insertVolunteerLog(volunteerLog);
        return success();
    }
}
